#ifndef SYS_TIMER_H
#define	SYS_TIMER_H

#include "mytype.h"
#include "stdint.h"

#ifdef   _TIMER_MODULE_
#define  TIM_EXT
#else
#define  TIM_EXT  extern
#endif      
    
#define SYSTICK_CYCLE             1000UL     //system tick timer interrupt cycle //us
#define COUNTER_10MS              ((uint8_t)((10UL*1000UL/SYSTICK_CYCLE)))         
#define COUNTER_20MS              ((uint8_t)((20UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_30MS              ((uint8_t)((30UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_50MS              ((uint8_t)((50UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_100MS             ((uint8_t)((100UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_200MS             ((uint8_t)((200UL*1000UL/SYSTICK_CYCLE)))
#define COUNTER_300MS             ((uint8_t)((250UL*1000UL/SYSTICK_CYCLE)))

#define COUNTER_125MS             ((uint8_t)((125UL*1000UL/SYSTICK_CYCLE)))
#define BS125MS_COUNTER_250MS     2
#define BS125MS_COUNTER_500MS     4
#define BS125MS_COUNTER_1S        8
#define BS125MS_COUNTER_2S        16
#define BS125MS_COUNTER_3S        24
#define BS125MS_COUNTER_10S       80
#define BS125MS_COUNTER_30S       240

TIM_EXT  uint8_t Counter_10ms;
TIM_EXT  uint8_t Counter_20ms;
TIM_EXT  uint8_t Counter_30ms;
TIM_EXT  uint8_t Counter_50ms;
TIM_EXT  uint8_t Counter_100ms;
TIM_EXT  uint8_t Counter_200ms;
TIM_EXT  uint8_t Counter_300ms;

TIM_EXT  uint8_t Counter_125ms;
TIM_EXT  uint8_t Counter_250ms;
TIM_EXT  uint8_t Counter_500ms;
TIM_EXT  uint8_t Counter_1s;
TIM_EXT  uint8_t Counter_2s;
TIM_EXT  uint8_t Counter_3s;
TIM_EXT  uint8_t Counter_10s;
TIM_EXT  uint8_t Counter_30s;

TIM_EXT  uint8_t  SysTickCnt;
TIM_EXT  uint8_t  AutoTimeCnt;                   //base on 100ms
TIM_EXT  uint32_t PowerupTimeCnt;                //base on 100ms


//--------------------state control group1 define end-------------------------//

//--------------------state control group2 define-----------------------------//
TIM_EXT volatile uint8_t PulseSt0;
TIM_EXT volatile uint8_t PulseSt1;
TIM_EXT volatile uint8_t WaveSt;

#define PULSE_10MS_POS   0x00
#define PULSE_20MS_POS   0x01
#define PULSE_30MS_POS   0x02
#define PULSE_50MS_POS   0x03
#define PULSE_100MS_POS  0x04
#define PULSE_200MS_POS  0x05
#define PULSE_300MS_POS  0x06
#define PULSE_TICK_POS   0x07

#define PULSE_125MS_POS  0x00
#define PULSE_250MS_POS  0x01
#define PULSE_500MS_POS  0x02
#define PULSE_1S_POS     0x03
#define PULSE_2S_POS     0x04
#define PULSE_3S_POS     0x05
#define PULSE_10S_POS    0x06
#define PULSE_30S_POS    0x07

#define WAVE_125MS_POS   0x00
#define WAVE_250MS_POS   0x01
#define WAVE_500MS_POS   0x02
#define WAVE_1S_POS      0x03
#define WAVE_2S_POS      0x04
#define WAVE_3S_POS      0x05
#define WAVE_10S_POS     0x06
#define WAVE_30S_POS     0x07

#define bPulse_10ms      BGET(PulseSt0 , PULSE_10MS_POS)
#define bPulse_20ms      BGET(PulseSt0 , PULSE_20MS_POS)
#define bPulse_30ms      BGET(PulseSt0 , PULSE_30MS_POS)
#define bPulse_50ms      BGET(PulseSt0 , PULSE_50MS_POS)
#define bPulse_100ms     BGET(PulseSt0 , PULSE_100MS_POS)
#define bPulse_200ms     BGET(PulseSt0 , PULSE_200MS_POS)
#define bPulse_300ms     BGET(PulseSt0 , PULSE_300MS_POS)
#define bPulse_Tick      BGET(PulseSt0 , PULSE_TICK_POS)

#define bPulse_125ms     BGET(PulseSt1 , PULSE_125MS_POS)
#define bPulse_250ms     BGET(PulseSt1 , PULSE_250MS_POS)
#define bPulse_500ms     BGET(PulseSt1 , PULSE_500MS_POS)
#define bPulse_1s        BGET(PulseSt1 , PULSE_1S_POS)
#define bPulse_2s        BGET(PulseSt1 , PULSE_2S_POS)
#define bPulse_3s        BGET(PulseSt1 , PULSE_3S_POS)
#define bPulse_10s       BGET(PulseSt1 , PULSE_10S_POS)
#define bPulse_30s       BGET(PulseSt1 , PULSE_30S_POS)

#define bWave_125ms      BGET(WaveSt , WAVE_125MS_POS)
#define bWave_250ms      BGET(WaveSt , WAVE_250MS_POS)
#define bWave_500ms      BGET(WaveSt , WAVE_500MS_POS)
#define bWave_1s         BGET(WaveSt , WAVE_1S_POS)
#define bWave_2s         BGET(WaveSt , WAVE_2S_POS)
#define bWave_3s         BGET(WaveSt , WAVE_3S_POS)
#define bWave_10s        BGET(WaveSt , WAVE_10S_POS)
#define bWave_30s        BGET(WaveSt , WAVE_30S_POS)

#define SET_PULSE(x)            BSET(PulseSt0,x)
#define CLR_PULSE(x)            BCLR(PulseSt0,x)
#define BS125MS_SET_PULSE(x)    BSET(PulseSt1,x)
#define BS125MS_CLR_PULSE(x)    BCLR(PulseSt1,x)
#define INV_WAVE(x)             BINV(WaveSt,x)

//--------------------state control group2 define end-------------------------//

void SysPulseCreat(void);
void SysPulseClear(void);
void SysTickInit(void);
void delay_us(uint32_t Cnt);
void delay_ms(uint32_t Cnt);

#undef TIM_EXT

#endif	/* SYS_TIMER_H */

